﻿Imports System.Data.OleDb
Imports System.Xml
Imports System.ServiceModel.Syndication

Partial Public Class DiscussionRss
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.ContentType = "application/rss+xml"

        Dim myFeed As New SyndicationFeed

        myFeed.Title = TextSyndicationContent.CreatePlaintextContent("Haunted Networking")
        myFeed.Description = TextSyndicationContent.CreatePlaintextContent _
        ("The five most recent Topics added to the haunted networking website")
        myFeed.Links.Add(SyndicationLink.CreateSelfLink _
        (New Uri(correctURL("discussionforum.aspx"))))
        myFeed.Language = "en-us"


        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("placesConnectionString").ToString())
        oleDbConn.Open()
        Dim cmdQuery As String = "SELECT TOP 5 * from discussionforumQuery"
        Dim dbcmd = New OleDbCommand(cmdQuery, oleDbConn)
        Dim dbRead = dbcmd.ExecuteReader()

        Dim myItems As New List(Of SyndicationItem)
        While dbRead.Read()
            Dim sItem As New SyndicationItem
            sItem.Title = makeRssText(dbRead("Topic"))
            sItem.Summary = makeRssText(dbRead("StartedBy"))
            sItem.Links.Add(makeRssLink("http://localhost:61093/discussionforum.aspx"))
            myItems.Add(sItem)
        End While

        myFeed.Items = myItems

        Dim feedWriter As XmlWriter = XmlWriter.Create(Response.OutputStream)

        Dim rssFormatter = New Rss20FeedFormatter(myFeed)

        rssFormatter.WriteTo(feedWriter)

        feedWriter.Close()



    End Sub

    Private Function makeRssLink(ByVal someLocation As String) As SyndicationLink
        Return SyndicationLink.CreateAlternateLink(New Uri(someLocation))
    End Function

    Private Function makeRssText(ByVal someString As String) As TextSyndicationContent
        Return TextSyndicationContent.CreatePlaintextContent(someString)
    End Function

    Private Function correctURL(ByVal urlStr As String)
        Return Request.Url.GetLeftPart(UriPartial.Authority) & ResolveUrl(urlStr)
    End Function

End Class